<template>
  <view class="perfect-info-logon">
    <customHeader class="perfect-info-logon_title" title="完善信息" />
    <perfectInfo
      :avatar-info="avatarInfo"
      :student="student"
      :ok="handleOk"
      @change-avatar="handleChangeAvatar"
    />
  </view>
</template>

<script>
import Taro from "@tarojs/taro";
import perfectInfo from "@/components/perfect-info/index";
import customHeader from "@/components/custom-header";
import { getDefaultAvatar } from "@/libs/api/base-api";
import { improveInformation } from "@/libs/api/login";
import basicDataSet from "@/config/basic-data-set";
import { getInviteId } from "@/libs/util";
export default {
  components: {
    perfectInfo,
    customHeader
  },
  data() {
    return {
      avatarInfo: {},
      student: {},
      hasChildren: false
    };
  },
  onLoad({ student }) {
    this.student = student ? JSON.parse(student) : {};
    this.avatarInfo = {
      avatar: this.student.avatar,
      id: this.student.avatarId
    };
  },
  methods: {
    handleChangeAvatar(data) {
      this.avatarInfo = data;
    },
    async handleOk(query) {
      if (this.isLoading) return;
      this.isLoading = true;
      /**邀请人id----用于邀请注册获取奖励 */
      const invite_user_id =
        Taro.getStorageSync("login_invite_id") || undefined;
      console.log({ invite_user_id }, "邀请获取奖励11111111");
      query = {
        ...this.student,
        ...query,
        avatar: this.avatarInfo.avatar,
        invite_user_id
      };
      console.log({ query }, "邀请获取奖励2222222222");
      const [err, { data: { token } = {} }] = await this.$captureCatch(
        improveInformation(query)
      );
      if (err) {
        this.isLoading = false;
        this.$message(err?.message);
        return;
      }
      /**拿到token 存放token */
      token && Taro.setStorageSync("token", token);
      /**清除掉邀请注册的学生id--否则可能会影响邀请组团的模块 */
      invite_user_id && Taro.removeStorageSync("login_invite_id");

      basicDataSet.setUser();
      /**返回页面需要刷新TODO: */
      Taro.navigateBack({
        delta: 2,
        complete: () => {
          this.isLoading = false;
        }
      });
    }
  }
};
</script>

<style lang="scss">
.perfect-info-logon_title {
  background-color: #ffffff;
}
</style>
